$(function (){

    //layui表格
    layui.use(['table','form','layer'], function(){
        var table = layui.table;
        var form = layui.form;
        var layer = layui.layer;
        
        
        var T = table.render({
            elem: '#Class_Course',
            url: '/selectClass_Course_All' //数据接口
            ,response : {
                statusCode : 200
                // 重新规定成功的状态码为 200，table 组件默认为 0
            },
            parseData : function(res) { // res 即为原始返回的数据
                return {
                    "count" : res.tol,
                    "code" : res.status, // 解析接口状态
                    "msg" : res.message, // 解析提示文本
                    "data" : res.list
                    // 解析数据列表
                };
            }
            ,cols: [[ //表头
                {field: 'classId', title: '班级编号', width:200,sort: true,align:'center', fixed: 'left'}
                ,{field: 'className', title: '班级名称',align:'center'}
                ,{field: 'courseId', title: '课程编号',align:'center' }
                ,{field: 'courseName',align:'center', title: '课程名称'}
                ,{field: 'tchId',align:'center', title: '授课教师工号'}
                ,{field: 'tchName',align:'center', title: '授课教师'}
                ,{field: 'operate',align:'center', toolbar : '#barDemo',title: '操作',width:180}
            ]]
        });


        
        
        table.on('tool(Class_Course)', function(obj){
            var data = obj.data;
            var classId = data.classId;
            var courseId = data.courseId;
            var tchId = data.tchId;
          //监听删除按钮
            if(obj.event == 'del'){
                layer.confirm('确定删除？', function(index){
                    $.ajax({
                        url:"/delete",
                        type:"post",
                        data:{
                        	'classId':classId,
                        	'courseId':courseId,
                        	'tchId':tchId
                        },
                        dataType:"json",
                        success:function (res) {
                        	if(res.msg == 'YES'){
                        		obj.del();
                                layer.close(index);
                                layer.msg("删除成功");
                        	}
                        },
                        error:function () {
                            layer.msg("error");
                            layer.close(index);
                        }
                    })

                });
            }
        });
    
        //院系按钮
        $.ajax({
			url : '/selectDept',
			dataType : 'json',
			type : 'post',
			success : function(data) {
				var da = eval(data);
				console.log(da);

				for (var i = 0; i < da.length; i++) {
					$("#department_select").append(
							"<button type=\"button\" class=\"btn btn-default\">"
									+ da[i].deptName
									+ "</button>");
				}
			},
			error : function() {
				layer.alert('院系信息读取失败', {
					icon : 2,
					skin : 'layer-ext-moon'
				})
			}
		})
		
		//点击院系按钮显示班级按钮及数据表中相应信息
		$("#department_select").on("click","button",function() {
											var deptName = $(this).text();
											console.log(deptName);
											$("#span1").html(deptName);
											$("#span2").html("全部班级");
											$("#select_input2").val("");
											if (deptName == "全部院系") {//当点击院系按钮是全部院系时
												$("#class_select>button:gt(0)").remove();
												layui.use('table',function() {
													var table = layui.table;
													table.reload('Class_Course',{
														url : '/selectClass_Course_All',
														
													})
												})
											} else {
												//当点击院系按钮不是全部院系是
												//1.查询班级
												$.ajax({
													url : 'CC_selectClass1',
													data : {
														"deptName" : deptName
													},
													dataType : 'json',
													type : 'post',
													success : function(data) {
														$("#class_select>button:gt(0)").remove();
														var da = eval(data);
														console.log(da);
														for (var i = 0; i < da.length; i++) {
															$("#class_select").append("<button type=\"button\" class=\"btn btn-default\">"+ da[i].className+ "</button>");
														}
													},
													error : function() {
														layer.alert('班级信息读取失败',{
															icon : 2,
															skin : 'layer-ext-moon'
														})
													}
												})
												//2.查询当前按钮信息的数据
												
												table.reload('Class_Course',{
													url : '/CC_selectMsgBy_deptName',
													where : {
														deptName : deptName
													},
															
												})

											
											}

										})
										
		//点击班级按钮
		$("#class_select").on("click", "button", function() {

							var className = $(this).text();
							$("#span2").html(className);
							var deptName = $("#span1").text();
							$("#select_input2").val("");
							
							console.log("点击班级按钮显示当前院系。。。。。。。。"+deptName);
							console.log("+++++++++++++");
							console.log(className);
							
							if(className == "全部班级" ){//当点击全部班级时
								layui.use('table', function() {
									var table = layui.table;
									table.reload('Class_Course', {
										url : '/CC_selectMsgBy_deptName',
										where : {
											deptName:deptName
										},
										
									})

								})
							}else{
								//当点击按钮不是全部班级时
								layui.use('table', function() {
									var table = layui.table;
									table.reload('Class_Course', {
										url : '/CC_selectMsgBy_className',
										where : {
											deptName:deptName,
											className : className
										},
										
									})

								})
							}
							
							

						})

		
        
        
        
	    //添加班级-课程
	    $("#Set_course_class").on("click",function () {
	        layer.open({
	            type: 1,
	            skin: 'layui-layer-rim', //加上边框
	            area: ['520px', '400px'], //宽高
	            content: '<form class="layui-form layui-form-pane" action="#">\n'
	            	+'  <br>\n'
	            	+'  <div class="layui-form-item">\n' 
	                +'    <label class="layui-form-label">院系名称</label>\n' 
	                + '    <div class="layui-input-block">\n'
					+ '      <select id="dept" name="deptName" lay-filter="dept" lay-verify="">\n'
					+ '        <option></option>\n'
					+ '      </select>\n'
					+ '    </div>\n'
					+'  </div>\n' 
	                +'  <div class="layui-form-item">\n' 
	                +'    <label class="layui-form-label">班级名称</label>\n' 
	                + '    <div class="layui-input-block">\n'
					+ '      <select id="select1" name="className" disabled lay-filter="select1" lay-verify="">\n'
					+ '        <option></option>\n'
					+ '      </select>\n'
					+ '    </div>\n'
					+'  </div>\n' 
	                +'  <div class="layui-form-item">\n' 
	                +'    <label class="layui-form-label">课程名称</label>\n' 
	                + '    <div class="layui-input-block">\n'
					+ '      <select id="select2" name="courseName" disabled lay-filter="select2" lay-verify="">\n'
					+ '        <option></option>\n'
					+ '      </select>\n'
					+ '    </div>\n'
	                +'  </div>\n' 
	                +'  <div class="layui-form-item">\n' 
	                +'    <label class="layui-form-label">授课教师</label>\n' 
	                + '    <div class="layui-input-block">\n'
					+ '      <select id="select3" name="tchName" disabled lay-filter="select3" lay-verify="">\n'
					+ '        <option></option>\n'
					+ '      </select>\n'
					+ '    </div>\n'
	                +'  </div>\n' 
	                +'<div ><button type="button" lay-submit lay-filter="btn1" class="layui-btn" style="margin-left: 160px;" id="button1">提交</button>\n' 
	                +'		<button type="reset" class="layui-btn layui-btn-primary" id="button2">重置</button></div>\n'
	                +'</form>'
	        });
	        //表单渲染
	        form.render();
	        
	        //添加  院系信息选择
	        $.ajax({
	        	url:'/selectDept',
	        	dataType:'json',
	        	type:'post',
	        	success:function(res){
	        		var d = eval(res);
					console.log(d);
					for (var i = 0; i < d.length; i++) {
						$("#dept").append("<option value="+d[i].deptId+">"+d[i].deptName+"</option>")
						console.log("......"+d[i].deptName+".."+d[i].deptId);
						form.render('select');
					}
	        	}
	        })
	        
	        //当院系选择后开启班级信息选择
	        form.on('select(dept)', function(data){
	        	var deptId = data.value;//得到被选中的值
				console.log(deptId);
	        	
				if(deptId != null && deptId !=''){
					$("#select1").attr("disabled",false);
					//添加班级下拉框内容
			        $.ajax({
			        	url:'/CC_selectClass',
			        	dataType:'json',
			        	type:'post',
			        	data:{
			        		'deptId':deptId
			        	},
			        	success:function(res){
			        		var d = eval(res);
							console.log(d);
							$("#select1").empty();
							$("#select1").append("<option></option>");
							$("#select2").empty();
							$("#select2").append("<option></option>");
							$("#select2").attr("disabled",true);
							$("#select3").empty();
							$("#select3").append("<option></option>");
							$("#select3").attr("disabled",true);
							form.render('select');
							if(d.length>0){
								for (var i = 0; i < d.length; i++) {
									$("#select1").append("<option value="+d[i].classId+">"+d[i].className+"</option>")
									console.log("......"+d[i].className+".."+d[i].classId);
									form.render('select');
								}
							}else{
								layer.msg("此院系下还没有班级，请到班级管理页面添加班级");
							}
							
			        	}
			        })
					
				}else{
					$("#select1").empty();
					$("#select1").append("<option></option>");
					$("#select1").attr("disabled",true);
					$("#select2").empty();
					$("#select2").append("<option></option>");
					$("#select2").attr("disabled",true);
					$("#select3").empty();
					$("#select3").append("<option></option>");
					$("#select3").attr("disabled",true);
					form.render('select');
				}
	        })
	        
	        
	        //选择班级后,开启课程选择框
	        form.on('select(select1)', function(data){
	        	var classId = data.value;
	        	console.log(classId);
	        	
	        	if(classId != null && classId !=''){
	        		
	        		$("#select2").attr("disabled",false);
	        		//添加课程下拉框内容
	                $.ajax({
	                	url:'/CC_selectCourse',
	                	dataType:'json',
	                	type:'post',
	                	success:function(res){
	                		var d = eval(res);
	        				console.log(d);
	        				$("#select2").empty();
							$("#select2").append("<option></option>");
							$("#select3").empty();
							$("#select3").append("<option></option>");
							$("#select3").attr("disabled",true);
							form.render('select');
							if(d.length>0){
								for (var i = 0; i < d.length; i++) {
		        					$("#select2").append("<option value="+d[i].courseId+">"+d[i].courseName+"</option>")
		        					console.log("......"+d[i].courseName+".."+d[i].courseId);
		        					form.render('select');
		        				}
							}else{
								layer.msg("此院系下还没有班级，请到班级管理页面添加班级");
							}
	        				
	                	}
	                })
	        	}else{
	        		$("#select2").empty();
					$("#select2").append("<option></option>");
					$("#select2").attr("disabled",true);
					$("#select3").empty();
					$("#select3").append("<option></option>");
					$("#select3").attr("disabled",true);
					form.render('select');
	        	}       	
	        })
	        
	        
	        //选中课程显示相应的教师
	        form.on('select(select2)', function(data){
				var select2 = data.value;//得到被选中的值
				var deptId =$("#dept").val();
				console.log(select2);
						 
				if(select2 != null && select2 !=''){
					$("#select3").attr("disabled",false);
					$.ajax({
						url :'/selectC_T',
						dataType : 'json',
						type : 'post',
						data:{
							"courseId":select2,
							"deptId":deptId
						},
						success : function(data) {
							var d = eval(data);
							console.log(d);
							$("#select3").empty();
							$("#select3").append("<option></option>");
							form.render('select');
							if(d.length>0){
								
								for (var i = 0; i < d.length; i++) {
									$("#select3").append("<option value="+d[i].tchId+">"+d[i].tchName+"</option>")
									console.log("......"+d[i].tchName+".."+d[i].tchId);
									form.render('select');
								}
							}else{
								layer.msg("本院系下的这门课程没有授课教师，请在课程管理页面添加教师");
							}
							
						},error:function(){
							layer.alert('系统错误',{
								icon : 2,
								skin : 'layer-ext-moon'
							})
						}
					})
				}else{
					$("#select3").empty();
					$("#select3").append("<option></option>");
					$("#select3").attr("disabled",true);
					form.render('select');
							  
				}
			}); 
	        
	        //提交
	        form.on('submit(btn1)', function(data){
				var formData = data.field;
				var deptId = formData.deptName;
				var classId = formData.className;
				var courseId = formData.courseName;
				var tchId = formData.tchName;
				
				if(deptId != '' && classId != '' && courseId !='' && tchId !=''){
					
					$.ajax({
						url:'/CC_insert',
						dataType:'json',
						type:'post',
						data:{
							'classId':classId,
							'courseId':courseId,
							'tchId':tchId
						},success:function(res){
							if(res.msg == "YES"){
								layer.closeAll();
								T.reload();
								layer.msg("添加成功");
							}else{
								layer.msg("这个班级已经存在这门课程了");
							}
						},error:function(){
							layer.alert('系统错误',{
								icon : 2,
								skin : 'layer-ext-moon'
							})
						}
						
					})
				}else{
					layer.alert('请将信息填写完整',{
						icon : 0,
						skin : 'layer-ext-moon'
					})
				}
				
				return false;
	        })
	    })
    
    
    
    
    });
})